//
//  DetailTableViewCell.m
//  TasteEnjoy
//
//  Created by XuRui on 16/5/9.
//  Copyright © 2016年 XuRui. All rights reserved.
//

#import "DetailTableViewCell.h"

@interface DetailTableViewCell ()<UIScrollViewDelegate>

//10
@property (nonatomic, retain) BaseView *viewBack; /**< 背景 */
@property (nonatomic, retain) BaseImageView *imgPhoto; /**< 图片 */
@property (nonatomic, retain) BaseLabel *labelContent; /**< 内容 */
@property (nonatomic, retain) BaseView *viewLine; /**< 线 */
@property (nonatomic, retain) BaseImageView *imgUserPhoto; /**< 用户头像 */
@property (nonatomic, retain) BaseLabel *labelUserName; /**< 用户名 */
@property (nonatomic, retain) BaseLabel *labelUserContent; /**< 收藏到 */
@property (nonatomic, retain) BaseLabel *labelTime; /**< 时间 */

@property (nonatomic, copy) NSString *imgWidth;/**<图片宽*/
@property (nonatomic, copy) NSString *imgHeight;/**<图片高*/

@property (nonatomic, retain) UIScrollView *scrollView; /**< 背景scrollview */
@property (nonatomic, assign) CGRect originalFrame; /**< 原大小 */
@property (nonatomic, retain) UIImageView *lastImageView; /**< 最近的img */

@end

@implementation DetailTableViewCell

#pragma mark - ------------ 内存管理 ---------------
- (void)dealloc
{
    [_viewBack release];
    [_imgPhoto release];
    [_labelContent release];
    [_viewLine release];
    [_imgUserPhoto release];
    [_labelUserName release];
    [_labelUserContent release];
    [_labelTime release];
    [_imgWidth release];
    [_imgHeight release];
    [_scrollView release];
    [_lastImageView release];
    [_dataDic release];
    [super dealloc];
    
}

#pragma mark - ------------ 初始化 ---------------
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self creatSubView];

    }
    return self;

}

- (void)creatSubView
{
    
    self.viewBack = [[BaseView alloc]init];
    [self.contentView addSubview:_viewBack];
    
    self.viewBack.layer.shadowOffset = CGSizeMake(0.1, 0.1);
    self.viewBack.layer.shadowOpacity = 0.3;
    self.viewBack.layer.shadowRadius = 1.5;
    
    
    //加tap手势 放大
    self.imgPhoto = [[BaseImageView alloc]init];
    [self.viewBack addSubview:_imgPhoto];
    _imgPhoto.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapImg = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showZoomImage:)];
    [_imgPhoto addGestureRecognizer:tapImg];
    [tapImg release];
    

    self.labelContent = [[BaseLabel alloc]init];
    [self.viewBack addSubview:_labelContent];
    _labelContent.numberOfLines = 0;
    _labelContent.font = [UIFont systemFontOfSize:14];
/** 夜间 */
    _labelContent.dk_textColorPicker = DKColorWithColors([UIColor grayColor],[UIColor lightGrayColor]);
    
    
    self.viewLine = [[BaseView alloc]init];
    [self.viewBack addSubview:_viewLine];
    
    
    self.imgUserPhoto = [[BaseImageView alloc]init];
    [self.viewBack addSubview:_imgUserPhoto];
    _imgUserPhoto.layer.cornerRadius = 25;
    _imgUserPhoto.clipsToBounds = YES;
  
  
    
    self.labelUserName = [[BaseLabel alloc]init];
    [self.viewBack addSubview:_labelUserName];
     _labelUserName.font = [UIFont systemFontOfSize:15];
    _labelUserName.textColor = [UIColor colorWithRed:0.00 green:0.61 blue:0.84 alpha:1.00];
    
    self.labelUserContent = [[BaseLabel alloc]init];
    [self.viewBack addSubview:_labelUserContent];
     _labelUserContent.font = [UIFont systemFontOfSize:14];
    _labelUserContent.textColor = [UIColor lightGrayColor];
    
    
    self.labelTime = [[BaseLabel alloc]init];
    [self.viewBack addSubview:_labelTime];
    _labelTime.textAlignment = NSTextAlignmentRight;
    _labelTime.font = [UIFont systemFontOfSize:13];
    _labelTime.textColor = [UIColor lightGrayColor];
    
    
   // _viewBack.backgroundColor = [UIColor whiteColor];
    /** 夜间 */
     _viewBack.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor] ,[UIColor grayColor]);
  //  _imgPhoto.backgroundColor = [UIColor yellowColor];
   // _labelContent.backgroundColor = [UIColor redColor];
    
    _viewLine.dk_backgroundColorPicker = DKColorWithColors([UIColor grayColor] ,[UIColor lightGrayColor]);
    
    //_imgUserPhoto.backgroundColor = [UIColor yellowColor];
   // _labelUserName.backgroundColor = [UIColor blueColor];
   // _labelTime.backgroundColor = [UIColor redColor];
   // _labelUserContent.backgroundColor = [UIColor orangeColor];
    

    [_viewBack release];
    [_imgPhoto release];
    [_labelContent release];
    [_viewLine release];
    [_imgUserPhoto release];
    [_labelUserName release];
    [_labelUserContent release];
    [_labelTime release];
    [_imgWidth release];
    [_imgHeight release];

}

#pragma mark - ------------ set赋值 ---------------
- (void)setDataDic:(NSDictionary *)dataDic
{

    if (_dataDic != dataDic) {
        [_dataDic release];
        _dataDic = [dataDic retain];
     
   
    self.imgWidth = _dataDic[@"photo"][@"width"];
    self.imgHeight = _dataDic[@"photo"][@"height"];
    
    [_imgPhoto sd_setImageWithURL:[NSURL URLWithString:[_dataDic[@"photo"][@"path"]stringByReplacingOccurrencesOfString:@"_webp" withString:@""]] placeholderImage:PLACEHOLDER];
    
    _labelContent.text = _dataDic[@"msg"];
    
    [_imgUserPhoto sd_setImageWithURL:[NSURL URLWithString:_dataDic[@"sender"][@"avatar"]]placeholderImage:PLACEHOLDER];
    _labelUserName.text = _dataDic[@"sender"][@"username"];
    _labelUserContent.text = [@"收集到 " stringByAppendingString: _dataDic[@"album"][@"name"]];
    _labelTime.text = _dataDic[@"add_datetime"];
   }
}

#pragma mark - ------------ layout 调用 ---------------
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //判断数据 如果不判断 会崩溃 'Constant is not finite!
    if (_dataDic) {
        
        [self creatFrame];
   }
}
#pragma mark - ------------ masonry设置frame ---------------
- (void)creatFrame
{
  
    [self trans];
   
    //获取图片高度
    CGFloat imgH = [[self class] heightWithPhoto:_imgWidth height:_imgHeight];
    //获取文字高度
    CGFloat textH = [[self class] heightWithText:_labelContent.text];
    
   //背景
    [_viewBack mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.left.mas_equalTo(self.contentView).offset(10);
        make.right.mas_equalTo(self.contentView).offset(-10);
        make.height.mas_equalTo( imgH + textH + 91);
    }];
    
    //图片
    [_imgPhoto mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.mas_equalTo(self.viewBack).offset(0);
        make.height.mas_equalTo(imgH);
    }];

    
    //文字
    [_labelContent mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(_imgPhoto.mas_bottom).offset(10);
        make.left.mas_equalTo(_viewBack).offset(10);
        make.right.mas_equalTo(_viewBack).offset(-10);
        make.height.mas_equalTo(textH);
    }];
    
    //线
    [_viewLine mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.mas_equalTo(self.viewBack).offset(0);
        make.top.mas_equalTo(_labelContent.mas_bottom).offset(10);
        make.height.mas_equalTo(1);
    }];
    
    //用户头像
    [_imgUserPhoto mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.viewLine.mas_bottom).offset(10);
        make.left.mas_equalTo(self.viewBack).offset(10);
        make.width.height.mas_equalTo(50);
        
    }];
    
    //用户名
    [_labelUserName mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(_imgUserPhoto);
        make.left.mas_equalTo(_imgUserPhoto.mas_right).offset(10);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(25);
    }];
    //收集到
    [_labelUserContent mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(_labelUserName.mas_bottom).offset(0);
        make.left.mas_equalTo(_labelUserName);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(_labelUserName);
    }];
    
    //时间
    [_labelTime mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(_imgUserPhoto);
        make.right.mas_equalTo(_viewBack).offset(-10);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(30);
        
    }];
    
}

#pragma mark - ------------ 点击图片 ---------------
-(void)showZoomImage:(UITapGestureRecognizer *)tap
{
    if (![(UIImageView *)tap.view image]) {
        return;
    }
    //scrollView作为背景
    UIScrollView *bgScrollView = [[UIScrollView alloc] init];
    bgScrollView.frame = [UIScreen mainScreen].bounds;
    bgScrollView.backgroundColor = [UIColor blackColor];
    bgScrollView.userInteractionEnabled = YES;
    
    /** 点击变大之后 在加tap手势 */
    UITapGestureRecognizer *tapBg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgView:)];
    [bgScrollView addGestureRecognizer:tapBg];
    
    //获取当前点击的img
    UIImageView *imgCurrent = (UIImageView *)tap.view;
    
    
    UIImageView *currntTapImg = [[UIImageView alloc] init];
    currntTapImg.image = imgCurrent.image; //照片设为一个
    //从contentView上 背景 scrolview 转换成当前照片坐标
    currntTapImg.frame = [bgScrollView convertRect:imgCurrent.frame fromView:self];
    [bgScrollView addSubview:currntTapImg];
    
    [[[UIApplication sharedApplication] keyWindow] addSubview:bgScrollView];
    
    self.lastImageView = currntTapImg;
    self.originalFrame = currntTapImg.frame;
    self.scrollView = bgScrollView;
    //最大放大比例
    self.scrollView.maximumZoomScale = 1.5;
    self.scrollView.delegate = self;
    
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame = currntTapImg.frame;
        frame.size.width = bgScrollView.frame.size.width;
        frame.size.height = frame.size.width * (currntTapImg.image.size.height / currntTapImg.image.size.width);
        frame.origin.x = 0;
        frame.origin.y = (bgScrollView.frame.size.height - frame.size.height) * 0.5;
        currntTapImg.frame = frame;
        if (frame.size.height > SCREEN_HEIGHT) {
            _scrollView.contentSize = CGSizeMake(frame.size.width, frame.size.height);
            frame.origin.x = 0;
            frame.origin.y = 0;
            currntTapImg.frame = frame;
        }
        
    }];
    
}

-(void)tapBgView:(UITapGestureRecognizer *)tapBgRecognizer
{
    self.scrollView.contentOffset = CGPointZero;
    [UIView animateWithDuration:0.5 animations:^{
        self.lastImageView.frame = self.originalFrame;
        tapBgRecognizer.view.backgroundColor = [UIColor clearColor];
    } completion:^(BOOL finished) {
        [tapBgRecognizer.view removeFromSuperview];
        self.scrollView = nil;
        self.lastImageView = nil;
    }];
}



#pragma mark - ------------ 返回缩放的视图 ---------------
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    
    
    return scrollView.subviews.firstObject;
}


#pragma mark - ------------ 图片自适应 ---------------
+ (CGFloat)heightWithPhoto:(NSNumber *)width height:(NSNumber *)height

{
    //图片高 = 屏幕宽度 * 图片高度 / 图片宽度
    CGFloat h = (SCREEN_WIDTH - 20) * height.doubleValue / width.doubleValue;
    return h;
}


#pragma mark - ------------ 文本高度自适应 ---------------

+ (CGFloat)heightWithText:(NSString *)text
{
    
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 40, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil];
    
    return rect.size.height;
    
}


#pragma mark - ------------ 自动布局设为no ---------------
- (void)trans
{
    _viewBack.translatesAutoresizingMaskIntoConstraints = NO;
    _imgPhoto.translatesAutoresizingMaskIntoConstraints = NO;
    _labelContent.translatesAutoresizingMaskIntoConstraints = NO;
    _viewLine.translatesAutoresizingMaskIntoConstraints = NO;
    _imgUserPhoto.translatesAutoresizingMaskIntoConstraints = NO;
    _labelUserName.translatesAutoresizingMaskIntoConstraints = NO;
    _labelUserContent.translatesAutoresizingMaskIntoConstraints = NO;
    _labelTime.translatesAutoresizingMaskIntoConstraints = NO;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
